//=============================================================================
// Scene_Shop.Window_ShopBuy
//=============================================================================

Window_ShopBuy.prototype.initialize = function(x, y, height, width, shopGoods) {
    Window_Selectable.prototype.initialize.call(this, x, y, width, height);
    this._shopGoods = shopGoods;
    this._money = 0;
    this.refresh();
    this.select(0);
    this._backgroundSprite = new Sprite();
    this._backgroundSprite.bitmap =
        ImageManager.loadBitmap('img/system/UI/', 'MenuBg_List');
    this.addChildAt(this._backgroundSprite, 0);
    this._backgroundSprite.x = -17;
    this._backgroundSprite.y = -1;
    this.createItemViewerWindow();
};


Window_ShopBuy.prototype.createItemViewerWindow = function(){
  const [x, y, width, height] = [80, 100, 240, 334]
  this._viewerWindow = new Window_ItemViewer(x, y, width, height)
  this._viewerWindow.hide()

  const curScene = SceneManager._scene;
  curScene.addWindow(this._viewerWindow)
}

Window_ShopBuy.prototype.showView = function(){
  if(!this._viewerWindow)return;
  const item = this.item()
  if(!item){
    // 老饕：无物品时隐藏
    this._viewerWindow.hide();
    return;
  }
  // 老饕：修改输入变量
  this._viewerWindow.showView(item);
}

Window_ShopBuy.prototype.hideView = function(){
  if(!this._viewerWindow)return;
  this._viewerWindow.hide()
}

// changeWindowWidth
Window_ShopBuy.prototype.windowWidth = function () {
    return 128;
};

// select
const originSelect = Window_ShopBuy.prototype.select
Window_ShopBuy.prototype.select = function (index) {
    originSelect.call(this, index)
    this.$emit('select', ...arguments)
}

// deselect
const originDeSelect = Window_ShopBuy.prototype.deselect
Window_ShopBuy.prototype.deselect = function () {
    originDeSelect.call(this)
    this.$emit('deselect', ...arguments)
}

const originRefresh = Window_ShopBuy.prototype.refresh
Window_ShopBuy.prototype.refresh = function() {
    originRefresh.call(this);
    this.showView();
    this.callUpdateHelp();
};


Window_ShopBuy.prototype.drawIcon = function (iconIndex, x, y, alpha = 255) {
    const bitmap = ImageManager.loadSystem('IconSet');
    const pw = Window_Base._iconWidth;
    const ph = Window_Base._iconHeight;
    const sx = iconIndex % 16 * pw;
    const sy = Math.floor(iconIndex / 16) * ph;

    // 应用带透明度的icon
    const opacityBitmap = bitmap.clone(bit => bit.paintOpacity = alpha)
    this.contents.blt(opacityBitmap, sx, sy, pw, ph, x, y)
}



// 不可用项以红色显示（老饕：因此方法影响装备窗口绘制，故将可用条件判定方法修改为isEnabled，可通用）
Window_ShopBuy.prototype.drawItemName = function (item, x, y, width) {
    width = width || 312
    if (item) {
        const iconBoxWidth = Window_Base._iconWidth + 4;
        this.resetTextColor();
        //if (!$gameParty.canUse(item)) {
        if (!this.isEnabled(item)){
            this.changeTextColor(this.hpGaugeColor1());
            this.drawIcon(item.iconIndex, x + 2, y + 2, 125);//alpha=125
        } else {
            this.drawIcon(item.iconIndex, x + 2, y + 2);
        }
        this.drawText(item.name.split("（")[0], x + iconBoxWidth, y, width - iconBoxWidth);
    }
}


Window_ShopBuy.prototype.drawItem = function(index) {
    var item = this._data[index];
    var rect = this.itemRect(index);
    var priceWidth = 96;
    rect.width -= this.textPadding();
    //this.changePaintOpacity(this.isEnabled(item));
    this.drawItemName(item, rect.x, rect.y, rect.width - priceWidth);


    // 绘制持有数
    if (Yanfly.Param.ItemQuantitySize) {
        this.contents.fontSize = Yanfly.Param.ItemQuantitySize;
    }

    const possessioNumWidth = this.textWidth('00');
    const possessionTextWidth = this.textWidth(TextManager.possession);
    const possessionWidth = possessioNumWidth + possessionTextWidth;
    this.changeTextColor(this.systemColor());
    this.drawText(TextManager.possession, rect.x + rect.width - possessionWidth,
        rect.y, possessionTextWidth);
    this.resetTextColor();
    this.drawText($gameParty.numItems(item), rect.x + rect.width - possessioNumWidth,
        rect.y, possessioNumWidth, 'right');
    


    this.drawText(this.price(item), rect.x + rect.width - priceWidth - possessionWidth - 32 - 8,
                  rect.y, priceWidth, 'right');
    this.drawIcon(Yanfly.Icon.Gold, rect.x + rect.width - possessionWidth - 32 + 2 - 8,
                    rect.y + 2);


     this.changePaintOpacity(true);
     this.resetFontSettings();
};




/*
  Window_ItemList.prototype.setStatusWindow = function (statusWindow) {
    this._statusWindow = statusWindow;
    this.update();
  };

  Window_ItemList.prototype.setInfoWindow = function (infoWindow) {
    this._infoWindow = infoWindow;
    this.update();
  };

  Yanfly.Item.Window_ItemList_updateHelp = Window_ItemList.prototype.updateHelp;
  Window_ItemList.prototype.updateHelp = function () {
    Yanfly.Item.Window_ItemList_updateHelp.call(this);
    if (SceneManager._scene instanceof Scene_Item && this._statusWindow) {
      this._statusWindow.setItem(this.item());
    }
    if (SceneManager._scene instanceof Scene_Item && this._infoWindow) {
      this._infoWindow.setItem(this.item());
    }
  };
  */

  // 如有问题看看YEP_ItemCore.js里的Window_ItemList